;;............................................................................... 
;; Create large-scale forcing data for SCAM5 
;; 
;; Source data : http://www.arm.gov/news/data/post/20319
;;               http://iop.archive.arm.gov/arm-iop/0eval-data/xie/scm-forcing/continuous_at_sgp/2010/?uid=ZHANGK1&st=5277daf7&home=arm-archive 
;;
;; Information from ARM website:   
;; 
;; UPDATE (07/23/2013) - New version for 2002-2010
;; 
;; The SGP-continuous large-scale forcing data has been updated for the period 
;; between 2002 and 2010 to improve its consistency in processing surface and TOA 
;; fluxes across this period and fix a bug in processing of the surface net 
;; radiation field for the period between November 2008 and June 2010, which 
;; includes RACORO and SPARTICUS campaign periods. Note that there are no changes 
;; in the forcing data for the period between 1999 and 2001. 
;; 
;; The major changes include:
;; 
;; (1) Surface net radiative flux is based on only the measurements from the Solar 
;; Infrared Radiation Station (SIRS) to improve the consistency between the 
;; net radiative flux and its individual components in the current version. 
;; The earlier version used the merged product from both SIRS and EBBR measurements, 
;; which was affected by a bug that was found in our processing of the EBBR 
;; net surface radiative flux for the period between November 2008 and June 2010.
;; 
;; (2) Optimized the scale length used in the Barnes analysis to fill in missing data 
;; for surface constraints.
;; 
;; These revisions have resulted in some considerable changes in the derived 
;; large-scale forcing data, particularly for the period between November 2008 
;; and June 2010. We strongly encourage users to re-run their experiments with 
;; the updated version of the data and provide feedback to us. Collected feedback 
;; will be used to improve and guide our future forcing data development.
;; 
;; The scm-forcing data are derived from the ARM Program observational data using the 
;; constrained variational analysis approach (Zhang and Lin 1997 and Zhang et al. 
;; 2001). The resulting products include both the large-scale forcing terms and 
;; the evaluation fields, which can be used for driving the Single-Column Models 
;; (SCMs) and Cloud Resolving Models (CRMs) and validating model simulations. 
;; Results from our studies are then used to improve cloud parameterizations in 
;; Global Climate Models (GCMs).
;; 
;; The continuous forcing data was derived using the hourly RUC-2 analyses (the 
;; NOAA rapid update cycle), which were constrained with the ARM ground surface 
;; measurements and the NOAA GOES-8 satellite data. Note that sounding data were 
;; not used in this data set.
;;  
;;............................................................................... 

  fna = "sgp_20100501_cam5.nc" 
  fnb = "sgp_20100401_shaocheng.nc" 

  load "loadit" 

  fla = addfile(fna,"w") 
  flb = addfile(fnb,"r") 


;;............................................................................... 
;; Variable name used in SCAM5 
;;............................................................................... 
  new_list = (/ "lev",                 \
                "Prec",                \  
                "lhflx",               \ 
                "shflx",               \ 
                "Ps",                  \ 
                "CF_Ps",               \ 
                "Tsair",               \ 
                "Tsoil",               \ 
                "RHsair",              \ 
                "windsrf",             \ 
                "usrf",                \ 
                "vsrf",                \ 
                "rad_net_srf",         \ 
                "lw_net_toa",          \ 
                "sw_net_toa",          \ 
                "sw_dn_toa",           \ 
                "cld_low",             \ 
                "cld_mid",             \ 
                "cld_high",            \ 
                "cld_tot",             \ 
                "cld_thick",           \ 
                "cld_top",             \ 
                "LWP",                 \ 
                "dh2odt_col",          \ 
                "h2o_adv_col",         \ 
                "evap_srf",            \ 
                "dsdt_col",            \ 
                "s_adv_col",           \ 
                "rad_heat_col",        \ 
                "LH_col",              \ 
                "omegas",              \ 
                "qs",                  \ 
                "s_srf",               \ 
                "prew",                \ 
                "srflwup",             \ 
                "srflwdn",             \ 
                "srfswup",             \ 
                "srfswdn",             \ 
                "Tskin",               \ 
                "alb",                 \ 
                "omega",               \ 
                "WD",                  \ 
                "divT",                \ 
                "vertdivTx",           \ 
                "divq",                \ 
                "vertdivq",            \ 
                "divs",                \ 
                "vertdivT",            \ 
                "dsdt",                \ 
                "dTdt",                \ 
                "dqdt",                \ 
                "q",                  \ 
                "q1",                  \ 
                "q2",                  \ 
                "REHUM"                /) 

;;............................................................................... 
;; Variable name used in original forcing data
;; 
;; http://www.arm.gov/news/data/post/20319 
;;............................................................................... 
  old_list = (/ "lev",                 \
                "prec_srf",            \
                "LH",                  \ 
                "SH",                  \ 
                "p_srf_aver",          \ 
                "p_srf_center",        \ 
                "T_srf",               \ 
                "T_soil",              \ 
                "RH_srf",              \ 
                "wspd_srf",            \ 
                "u_srf",               \ 
                "v_srf",               \ 
                "rad_net_srf",         \ 
                "lw_net_toa",          \ 
                "sw_net_toa",          \ 
                "sw_dn_toa",           \ 
                "cld_low",             \ 
                "cld_mid",             \ 
                "cld_high",            \ 
                "cld_tot",             \ 
                "cld_thick",           \ 
                "cld_top",             \ 
                "LWP",                 \ 
                "dh2odt_col",          \
                "h2o_adv_col",         \
                "evap_srf",            \
                "dsdt_col",            \
                "s_adv_col",           \
                "rad_heat_col",        \
                "LH_col",              \
                "omega_srf",           \ 
                "q_srf",               \
                "s_srf",               \
                "PW",                  \ 
                "lw_up_srf",           \ 
                "lw_dn_srf",           \ 
                "sw_up_srf",           \ 
                "sw_dn_srf",           \ 
                "T_skin",              \ 
                "missing",             \ 
                "omega",               \ 
                "div",                 \ 
                "T_adv_h",             \ 
                "T_adv_v",             \ 
                "q_adv_h",             \ 
                "q_adv_v",             \ 
                "s_adv_h",             \ 
                "s_adv_v",             \ 
                "dsdt",                \ 
                "dTdt",                \ 
                "dqdt",                \ 
                "q",                   \ 
                "q1",                  \ 
                "q2",                  \ 
                "missing"              /) 

  ns = dimsizes(new_list) 
  ms = dimsizes(old_list) 

  if(ns .ne. ms) then 
     print("check new_list and old_list !!!") 
     exit 
  end if 

;;............................................................................... 
;; Loop over variables 
;;............................................................................... 
  do is = 0, ns-1 

     if(old_list(is) .ne. "missing") then 

     va = fla->$old_list(is)$
     vb = flb->$new_list(is)$

     print(" name : "+old_list(is)+"  "+new_list(is)) 
     ;;print("old unit : "+va@long_name+" "+va@units+"  new unit : "+vb@long_name+" "+vb@units) 

     va@long_name = vb@long_name
     va@units = vb@units
    
     ;;.........................................
     ;; /h -> /s
     ;;.........................................
     if(new_list(is).eq."Prec") then 
        va = va / 3600. 
     end if 

     ;;.........................................
     ;; /h -> /s
     ;;.........................................
     if(new_list(is).eq."evap_srf") then 
        va = va / 3600. 
     end if 

     ;;.........................................
     ;; hPa -> Pa
     ;;.........................................
     if(new_list(is).eq."Ps" .or. \
        new_list(is).eq."lev" .or. \
        new_list(is).eq."CF_Ps") then 
        va = va * 100. 
     end if 

     ;;.........................................
     ;; C -> K 
     ;;.........................................
     if(new_list(is).eq."Tsair") then 
        va = va + 273.15  
     end if 

     ;;.........................................
     ;; C -> K 
     ;;.........................................
     if(new_list(is).eq."Tsoil") then 
        va = va + 273.15  
     end if 

     ;;.........................................
     ;; g/kg -> kg/kg  
     ;;.........................................
     if(new_list(is).eq."q") then 
        va = va / 1000.
     end if 

     ;;.........................................
     ;; km -> m
     ;;.........................................
     if(new_list(is).eq."cld_thick") then 
        va = va * 1000.
     end if 

     ;;.........................................
     ;; km -> m
     ;;.........................................
     if(new_list(is).eq."cld_top") then 
        va = va * 1000.
     end if 

     ;;.........................................
     ;; 
     ;;.........................................
     if(new_list(is).eq."LWP") then 
        va = va / 100.
     end if 

     ;;.........................................
     ;; /h -> /s  
     ;;.........................................
     if(new_list(is).eq."omega") then 
        va = va * 100. / 3600. 
     end if 

     ;;.........................................
     ;; W/m2 -> K 
     ;;.........................................
     if(new_list(is).eq."dsdt_col") then 
        va = va 
     end if 

     ;;.........................................
     ;; K/hour -> k/s 
     ;;.........................................
     if(new_list(is).eq."divT" .or. \
        new_list(is).eq."vertdivTx" .or. \
        new_list(is).eq."dh2odt_col" .or. \
        new_list(is).eq."h2o_adv_col" .or. \
        new_list(is).eq."q1" .or. \
        new_list(is).eq."q2" .or. \
        new_list(is).eq."dsdt" .or. \
        new_list(is).eq."dTdt" .or. \
        new_list(is).eq."divs" .or. \
        new_list(is).eq."vertdivT" .or. \
        new_list(is).eq."dqdt") then 
        va = va / 3600. 
     end if 

     ;;.........................................
     ;; g/kg/hour -> kg/kg/s 
     ;;.........................................
     if(new_list(is).eq."divq" .or. \
        new_list(is).eq."dqdt" .or. \
        new_list(is).eq."vertdivq" ) then 
        va = va / 1000. / 3600. 
     end if 

     fla->$new_list(is)$=va

     delete(va) 
     delete(vb) 

     end if

  end do

;;;;............................................................................... 
;;;; add bdate 
;;;;............................................................................... 
;;  ;;bdate = 20100401
;;  ;;bdate@long_name = "baseline date" ;
;;  ;;bdate@units = "yymmdd" ;
;;  
;;  bdate = flb->bdate
;;  bdate = (/ 20091201 /) 
;;  fla->bdate=bdate 
;;
;;  ;;tsec = flb->tsec
;;  ;;fla->tsec=tsec
;;
;;;;............................................................................... 
;;;; add lat & lon 
;;;;............................................................................... 
;;  lat = 36.605 
;;  lat!0 = "lat" 
;;  lat&lat = 36.605
;;
;;  lon = -97.485
;;  lon!0 = "lon" 
;;  lon&lon = -97.485
;; 
;;  fla->lon=lon
;;  fla->lat=lat
  



 
